Relieving data marshalling overhead

ABSTRACT

Data may be marshalled from a managed runtime environment to an unmanaged environment by using a compiler to calculate argument offsets and generate marshalling language code.

BACKGROUND OF THE INVENTION

The number of applications under the control of managed runtime environments (MTREs), such as the Java Virtual Machine (JVM) and the Common Language Runtime (CLR), is increasing. These applications may need access to system components outside of the managed runtime environment. Such components may include legacy applications or components that provide access to platform-specific services. In the CLR, this kind of native code access may be performed by an invocation method that may have an overhead of between 10 and 30×86 instructions per call, for example. In addition to this fixed cost, if the parameters or the return value of the called function need to be marshalled, the marshalling may create additional overhead depending on the type and size of the data, for example.

As the developer community migrates towards MTREs, unmanaged software components will need to be integrated into these new MTRE-based systems. However, low-level platform components and legacy applications will not be managed by these MTREs for quite some time. Several approaches for integrating these low-level components and legacy applications fail to provide an efficient and easy-to-use mechanism for allowing managed and unmanaged code to interact.

BRIEF DESCRIPTION OF THE DRAWINGS

Various exemplary features and advantages of embodiments of the invention will be apparent from the following, more particular description of exemplary embodiments of the present invention, as illustrated in the accompanying drawings wherein like reference numbers generally indicate identical, functionally similar, and/or structurally similar elements.

FIG. 1 depicts an exemplary embodiment of a system according to an exemplary embodiment of the invention;

FIG. 2 depicts an exemplary embodiment of a method according to an exemplary embodiment of the invention; and

FIG. 3 depicts an exemplary embodiment of a computer and/or communications system as may be used for several components in an exemplary embodiment of the invention.

DETAILED DESCRIPTION OF EXEMPLARY EMBODIMENTS OF THE PRESENT INVENTION

Exemplary embodiments of the invention are discussed in detail below. While specific exemplary embodiments are discussed, it should be understood that this is done for illustration purposes only. A person skilled in the relevant art will recognize that other components and configurations may be used without parting from the spirit and scope of the invention.

Exemplary embodiments of the present invention may provide a system and method for efficient data marshalling. As will be understood by a person having ordinary skill in the art, runtime applications or managed functions may need to execute native or unmanaged code. For example, a C# application or function may need to execute C++ or C code within a runtime application. When an application accesses code outside of the managed function, for example, data may be marshalled between the managed code and the unmanaged code. As will be understood by a person having ordinary skill in the art, a stub may provide a link between the managed and unmanaged code. As referred to herein, data marshalling may be the passing of data between managed code and unmanaged code.

FIG. 1 depicts an exemplary embodiment of a managed runtime architecture 100 for data marshalling according to exemplary embodiments of the invention. As shown in FIG. 1, data marshalling architecture 100 may include an application module 101, a compiler 102, and a common language runtime (CLR) environment 103. In an exemplary embodiment of the invention, application module 101 may store applications that may be executed within the CLR environment 103, for example. In an exemplary embodiment of the invention, these applications may need access to system components outside CLR environment 103 when they are executed.

As shown in FIG. 1, application module 101 may be coupled to compiler 102. In an exemplary embodiment of the invention, compiler 102 may analyze the applications that may be stored in application module 101 to create an intermediate language (IL) code. In other words, compiler 102 may compile the managed code of the applications that may be stored in application module 102. As will be understood by a person having ordinary skill in the art, in an exemplary embodiment of the invention, compiler 102 may be a C# (C-sharp) compiler.

In an exemplary embodiment of the invention, compiler 102 may be coupled to CLR environment 103. As will be understood by a person having ordinary skill in the art, CLR environment 103 may provide a virtual environment to execute applications in the runtime.

As discussed above, data marshalling may be a part of the calling of an unmanaged function within a managed runtime environment, for example. As will be understood by a person having ordinary skill in the art, to call an unmanaged function, CLR may 1) load a data link library (DLL) into memory and locate the address of the unmanaged function; 2) push arguments onto a stack and marshall arguments from the stack as needed; and 3) transfer control to the unmanaged function. In an exemplary embodiment of the invention, the managed runtime environment may use a native stub to marshall data, for example.

FIG. 2 depicts flow chart 200, which illustrates an exemplary method for executing managed and unmanaged code using data marshalling according to exemplary embodiments of the invention. In block 201 arguments may be calculated. In an exemplary embodiment of the invention, a compiler, such as a C# compiler may calculate the argument offsets. As will be understood by a person having ordinary skill in the art, argument offsets may allow the managed runtime environment to understand the size of the arguments so that the arguments may be placed on a call stack.

In block 202, marshalling language (ML) code, which may be an intermediate code that a CLR developer may easily define, may be generated. In an exemplary embodiment of the invention, the ML code may be generated by the C# compiler, for example.

In block 203, the generated ML code may be set into assembly, for example by the C# compiler. As referred to herein, assembly may be a configured set of loadable code modules and other resources that together implement a unit of functionality.

In an exemplary embodiment of the invention, the ML code may be set into assembly together with metadata, for example, and other intermediate language code.

In block 204, a native library and DLL entry point may be loaded by a CLR, for example.

In block 205, a native stub may be created by the CLR, for example.

In block 206, the native stub may be executed and control may be changed to the native function.

FIG. 3 depicts an exemplary embodiment of a computer and/or communications system as may be used for several components of the system in an exemplary embodiment of the present invention. FIG. 3 depicts an exemplary embodiment of a computer 300 as may be used for several computing devices in exemplary embodiments of the present invention. Computer 300 may include, but is not limited to: e.g., any computer device, or communications device including, e.g., a personal computer (PC), a workstation, a mobile device, a phone, a handheld PC, a personal digital assistant (PDA), a thin client, a fat client, an network appliance, an Internet browser, a paging, or alert device, a television, an interactive television, a receiver, a tuner, a high definition (HD) television, an HD receiver, a video-on-demand (VOD) system, a server, or other device.

Computer 300, in an exemplary embodiment, may comprise a central processing unit (CPU) or processor 304, which may be coupled to a bus 302. Processor 304 may, e.g., access main memory 306 via bus 302. Computer 300 may be coupled to an Input/Output (I/O) subsystem such as, e.g., a network interface card (NIC) 322, or a modem 324 for access to network 326. Computer 300 may also be coupled to a secondary memory 308 directly via bus 302, or via main memory 306, for example. Secondary memory 308 may include, e.g., a disk storage unit 310 or other storage medium. Exemplary disk storage units 310 may include, but are not limited to, a magnetic storage device such as, e.g., a hard disk, an optical storage device such as, e.g., a write once read many (WORM) drive, or a compact disc (CD), or a magneto optical device. Another type of secondary memory 308 may include a removable disk storage device 312, which may be used in conjunction with a removable storage medium 314, such as, e.g. a CD-ROM, or a floppy diskette. In general, the disk storage unit 310 may store an application program for operating the computer system referred to commonly as an operating system. The disk storage unit 310 may also store documents of a database (not shown). The computer 300 may interact with the I/O subsystems and disk storage unit 310 via bus 302. The bus 302 may also be coupled to a display 320 for output, and input devices such as, but not limited to, a keyboard 318 and a mouse or other pointing/selection device 316.

The embodiments illustrated and discussed in this specification are intended only to teach those skilled in the art various ways known to the inventors to make and use the invention. Nothing in this specification should be considered as limiting the scope of the present invention. All examples presented are representative and non-limiting. The above-described embodiments of the invention may be modified or varied, without departing from the invention, as appreciated by those skilled in the art in light of the above teachings. It is therefore to be understood that the invention may be practiced otherwise than as specifically described. 

1. A method comprising: calculating argument offset to pass arguments from managed code to unmanaged code; generating marshalling language code; setting the generated marshalling language code into assembly; loading a native library and a data link library entry point; creating a native stub; and executing the native stub.
 2. The method according to claim 1, wherein said calculating, generating, setting, loading, creating, and executing are carried out in the order as claimed.
 3. The method according to claim 1, wherein said calculating and generating is performed by a C# compiler.
 4. The method according to claim 1, said setting further comprising setting the generated marshalling language code with at least one of metadata or intermediate language code.
 5. The method according to claim 1, wherein said loading, creating, and executing are performed by a common language runtime.
 6. A system comprising: a compiler to calculate argument offsets and generate data marshalling language code a common language runtime environment to load a native library and native code entry point, create a native stub, and execute the native stub.
 7. The system according to claim 6, wherein the compiler is a C# compiler.
 8. The system according to claim 6, wherein the compiler first calculates the argument offsets and generates the data marshalling language code, and the common language runtime environment subsequently loads the native library and native code entry point, creates the native stub, and executes the native stub.
 9. A machine accessible medium containing program instructions that, when executed by a processor, cause the processor to perform a series of operations comprising: calculating argument offsets to marshall data from a managed runtime environment to an unmanaged environment; generating marshalling language code; setting the generated marshalling language code into assembly; loading a native library and a native code entry point; creating a native stub; and executing the native stub.
 10. The machine accessible medium according to claim 9, further containing program instructions that, when executed by the processor cause the processor to perform further operations comprising: transfer control from the managed runtime environment to the unmanaged environment.
 11. The machine accessible medium according to claim 9, wherein said calculating, generating, setting, loading, creating, and executing are executed by the processor in the order as claimed.
 12. The machine accessible medium according to claim 9, wherein said processor is furnished with a compiler and a common language runtime environment, and wherein said program instructions, when executed by said processor, cause said compiler to perform said calculating, generating, and setting and said common language runtime environment to perform said loading, creating, and executing.
 13. A method comprising: calculating argument offset to pass arguments from managed code to native code; generating marshalling language code; setting the generated marshalling language code into assembly; loading a native library and a data link library entry point; creating a native stub; and accessing the native code.
 14. The method according to claim 13, wherein said calculating, generating, setting, loading, creating, and accessing are carried out in the order as claimed.
 15. The method according to claim 13, wherein said calculating and generating is performed by a C# compiler.
 16. A machine accessible medium containing program instructions that, when executed by a processor, cause the processor to perform a series of operations comprising: calculating argument offsets to marshall data from managed code to native code; generating marshalling language code; setting the generated marshalling language code into assembly; loading a native library and a native code entry point; creating a native stub; and accessing the native code.
 17. The machine accessible medium according to claim 16, wherein said calculating, generating, setting, loading, creating, and accessing are executed by the processor in the order as claimed.
 18. The machine accessible medium according to claim 16, wherein said processor is furnished with a compiler and a common language runtime environment, and wherein said program instructions, when executed by said processor, cause said compiler to perform said calculating, generating, and setting and said common language runtime environment to perform said loading, creating, and accessing. 